package web

import (
	errorCode "ShortLink/Data/ErrorCode"
	models "ShortLink/Data/Models"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

// 请求成功
func Success(c *gin.Context) {
	result := new(models.APIResult)
	result.Code = errorCode.Success
	result.Msg = "请求成功"
	c.AbortWithStatusJSON(http.StatusOK, result)
}

// 参数错误
func ParamError(c *gin.Context, msg ...string) {
	result := new(models.APIResult)
	result.Code = errorCode.ParamError
	result.Msg = strings.Join(msg, "")
	if len(result.Msg) == 0 {
		result.Msg = "参数错误"
	}
	c.AbortWithStatusJSON(http.StatusBadRequest, result)
}

// 请求失败
func ServerError(c *gin.Context) {
	result := new(models.APIResult)
	result.Code = errorCode.Success
	result.Msg = "请求失败"
	c.AbortWithStatusJSON(http.StatusInternalServerError, result)
}

func NotExist(c *gin.Context, msg ...string) {
	result := new(models.APIResult)
	result.Code = errorCode.DataNotExist
	result.Msg = strings.Join(msg, "")
	if len(result.Msg) == 0 {
		result.Msg = "数据不存在"
	}
	c.AbortWithStatusJSON(http.StatusInternalServerError, result)
}
